<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends Controller {
	protected $admin;
	protected $config;
	public function _initialize(){
		
		if (empty($_SESSION['ADMIN_KEY'])){
			$this->redirect('Login/login');
		}
		
		
		//管理员信息
		$uid=$_SESSION['admin_userid'];
		$admin_user=M("Admin")->where("admin_id='$uid'")->find();
		$this->admin=$admin_user;
		//**************权限管理***********
		/* $URL_MODULE_MAP = array_keys(C('URL_MODULE_MAP'))[0];
		$adminquanxian=M("Admin")->field('Nav')->where("admin_id='{$_SESSION['admin_userid']}'")->find();
		$rules=$adminquanxian['nav'];
		if(empty($rules)){
			$this->error('此账号尚未分配权限',$URL_MODULE_MAP.'/Login/login');
		}
		$rules=explode(',', $rules);
		foreach ($rules as $k=>$v){
			$list[]=M("Nav")->where('nav_id='.$v)->find();
		}
		foreach ($list as $k=>$v){
			$v['nav_url'] = '/'.$URL_MODULE_MAP.$v['nav_url'];
			$value[$v['cat_id']][]=$v;
		}
		foreach ($value as $k=>$v){
			$lisst[$k."_nav"] = $v;
			$this->assign($k."_nav",$v);
		}
		$this->assign('URL_MODULE_MAP',$URL_MODULE_MAP); */
	}
	
	/**
	*根据会员ID查询会员姓名     返回值：str => 会员姓名
	*/
	protected function getMember_NameById($id){
		if(empty($id)){
			return null;
		}
		$re = M('Member')->where("member_id = {$id}")->find();
		if(!empty($re['name'])){
			return $re['name'];
		}else{
			return null;
		}
		
	}
	
	/**
	*图片上传方法
	*参数：$_FILES['name'] 数组
	*返回值：图片访问路径
	**/
	protected function imgUpload($file){
		 switch($file['type'])
	    {
	        case 'image/jpeg': $ext = 'jpg'; break;
	        case 'image/gif': $ext = 'gif'; break;
	        case 'image/png': $ext = 'png'; break;
	        case 'image/tiff': $ext = 'tif'; break;
	        default: $ext = ''; break;
	    }
	    if (empty($ext)){
	        return false;
	    }
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     9999999999 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'tif');// 设置附件上传类型
		$upload->saveExt   =     $ext;
		$upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
		// 上传文件
		$info   =  $upload->uploadOne($file);
		if(!$info) {
			// 上传错误提示错误信息
			$this->error($upload->getError());exit();
		}else{
			// 上传成功
			$pic=$info['savepath'].$info['savename'];
			$url='/Uploads/'.ltrim($pic,".");
		}
		return $url;
	}
	/**
	*加入消息
	*/
	public function addMessage($member_id,$title,$content){
		if(empty($member_id) || empty($title) || empty($content)){
			return false;
		}
		$data['member_id'] = $member_id;
		$data['message_all_id'] = 0;
		$data['title'] = $title;
		$data['content'] = $content;
		$data['add_time'] = time();
		$data['status'] = 0;
		
		$re = M('Message')->add($data);
		if($re){
			return true;
		}else{
			return false;
		}
	}
/***************************************************************Next is Old***************************************************************************************/	
	/**
	 * 权限管理
	 * @return boolean
	 */
	protected function getAuth() {
		$sess_user_id = session('admin_userid');
		// 		$sess_user_id = session ( 'USER_KEY_ID' );
		if (empty ( $sess_user_id )) {
			$this->redirect ( "Base/Login/index" );
		}
		if ($sess_user_id == 1) {
			return true;
		}
		$Auth = new \Think\Auth ();
		// 需要验证的规则列表,支持逗号分隔的权限规则或索引数组
		//MODULE_NAME . '/' .
		$name =  CONTROLLER_NAME .'/' . ACTION_NAME;
		// 当前用户id
		$uid = $sess_user_id;
		// 分类
		$type = MODULE_NAME;
		// 执行check的模式
		$mode = 'url';
		// 'or' 表示满足任一条规则即通过验证;
		// 'and'则表示需满足所有规则才能通过验证
		$relation = 'and';
		if (! $Auth->check ( $name, $uid, $type, $mode, $relation )) {
			if (IS_AJAX) {
				$data ['status'] = -1;
				$data ['info'] = "您没有权限访问";
				$this->ajaxReturn ( $data );
				exit ();
			} else {
				$this->error ( "您没有权限访问" );
			}
		}
	}
	//后台左侧导航栏
	public function qx_now(){
		$member_id = session('admin_userid');
		$res_group = M('Auth_group_access')->getByUid($member_id);
		$res_rules = M('Auth_group')->getById($res_group['group_id']);
		$rules = explode(',',$res_rules['rules']);
		$where['id'] = array('in',$rules);
		$where['pid'] = 0;
		$res_qx = M('Auth_rule')->where($where)->field('name,id,title')->select();
		foreach ($res_qx as $k=>&$v){
			$v['son'] = M('Auth_rule')->field('name,title')->where(array('xianshi'=>1,'pid'=>$v['id'],'id'=>array('in',$rules)))->select();
		}
		$this->assign('list',$res_qx);
	}
	//图片处理
	public function upload($file){
	    
	    switch($file['type'])
	    {
	        case 'image/jpeg': $ext = 'jpg'; break;
	        case 'image/gif': $ext = 'gif'; break;
	        case 'image/png': $ext = 'png'; break;
	        case 'image/tiff': $ext = 'tif'; break;
	        default: $ext = ''; break;
	    }
	    if (empty($ext)){
	        return false;
	    }
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     3145728 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath  =      './Public/Uploads/'; // 设置附件上传目录
		// 上传文件
		$info   =  $upload->uploadOne($file);
		if(!$info) {
			// 上传错误提示错误信息
			$this->error($upload->getError());exit();
		}else{
			// 上传成功
			$pic=$info['savepath'].$info['savename'];
			$url='/Uploads'.ltrim($pic,".");
		}
		return $url;
	}
	/**
	*加入日志的方法
	参数    $member_id => 会员ID    $finance_category_id =>日志类别ID    $content=>日志内容    
	返回值：   成功返回TRUE    失败返回FALSE
	*/

	public function upload_art($file){
			
		// 		switch($file['type'])
		// 		{
		// 			case 'image/jpeg': $ext = 'jpg'; break;
		// 			case 'image/gif': $ext = 'gif'; break;
		// 			case 'image/png': $ext = 'png'; break;
		// 			case 'image/tiff': $ext = 'tif'; break;
		// 			default: $ext = ''; break;
		// 		}
		// 		if (empty($ext)){
		// 			return false;
		// 		}
	
		if($file['type'] != "application/msword"){
			return false;
		}
		$ext = "doc";
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     3145728 ;// 设置附件上传大小
		// 		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->exts      =     array('doc');
		$upload->savePath  =      './Public/Uploads/'; // 设置附件上传目录
		// 上传文件
	
		$info   =  $upload->uploadOne($file);
		if(!$info) {
			// 上传错误提示错误信息
			$this->error($upload->getError());exit();
		}else{
			// 			dump($file);die;
			// 上传成功
			$pic=$info['savepath'].$info['savename'];
			$url='/Uploads'.ltrim($pic,".");
		}
		return $url;
	}
	//空操作
	public function _empty(){
		header("HTTP/1.0 404 Not Found");
		$this->display('Public:404');
	}
	//财务日志
	public function addFinance($openid,$sz_type,$type,$money,$content){
	    $data['open_id'] = $openid;
	    if(!$data['open_id']){
	        $this->ajaxReturn(['status'=>-1,'msg'=>'系统繁忙，请稍后再试'],'JSONP');
	    }
	    $data['sz_type'] = $sz_type;
	    $data['type']    = $type;
	    $data['content'] = $content;
	    $data['money'] = $money;
	    $data['addtime'] = time();
	    $r = M('Finance')->add($data);
	    if($r){
	        return true;
	    }else{
	        return false;
	    }
	}
	//查财务日志
	public function selectFinance($type){
	    //$type  1余额 2 积分
	    $list = M('Finance')->where('type ='.$type)->select();
	    foreach($list as $k=>$v){
	        if($v['sz_type'] == 1){
	            $list[$k]['sz_type'] = "收入";
	        }
	        if($v['sz_type'] == 2){
	            $list[$k]['sz_type'] = "支出";
	        }
	        if($v['type'] == 2){
	           $list[$k]['type'] = "积分";
	        }
	        if($v['type'] == 1){
	            $list[$k]['type'] = "余额";
	        }
	    }
	    return $list;
	}
}



